//
//  DayNumPickerView.m
//  Learn
//
//  Created by apple on 2024/4/13.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "DayNumPickerView.h"
#import "BRPickerView.h"

@interface DayNumPickerView ()
/* <#描述#> */
@property (nonatomic, strong) BRStringPickerView    * dayPickerView;
@property (nonatomic, strong) BRStringPickerView    * countPickerView;
@property (nonatomic, strong) NSMutableArray    * dayArray;
@property (nonatomic, strong) NSMutableArray    * countArray;

@end

@implementation DayNumPickerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self cretaeUI];
    }
    return self;
}

- (void)cretaeUI {
    UIView *bg1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width/2, self.height)];
    [self addSubview:bg1];
    UIView *bg2 = [[UIView alloc]initWithFrame:CGRectMake(self.width/2, 0, self.width/2, self.height)];
    [self addSubview:bg2];
    
    self.dayPickerView = [[BRStringPickerView alloc]init];
    self.dayPickerView.pickerMode = BRStringPickerComponentSingle;
    WS(WeakSelf)
    self.dayPickerView.changeModelBlock = ^(BRResultModel * _Nullable resultModel) {
        [WeakSelf cheackCountPickerWithModel:resultModel];
    };
    self.countPickerView = [[BRStringPickerView alloc]init];
    self.countPickerView.pickerMode = BRStringPickerComponentSingle;
    self.countPickerView.changeModelBlock = ^(BRResultModel * _Nullable resultModel) {
        [WeakSelf cheackDayPickerWithModel:resultModel];
    };
    // 设置选择器中间选中行的样式
    BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
    customStyle.hiddenCancelBtn = YES;
    customStyle.hiddenDoneBtn = YES;
    customStyle.hiddenTitleBarView = YES;
    customStyle.rowHeight = 44;
    customStyle.selectRowTextFont = PingFangMediumFont(16);
    customStyle.selectRowTextColor = COLOR_TOPIC_SELECT;
    
    self.dayPickerView.pickerStyle = customStyle;
    self.countPickerView.pickerStyle = customStyle;
    
    [self.dayPickerView addPickerToView:bg1];
    [self.countPickerView addPickerToView:bg2];
}

- (void)cheackCountPickerWithModel:(BRResultModel*)model{
    int count = (ceilf)(self.allCount.floatValue/model.value.floatValue);
    NSString *countStr = [NSString stringWithFormat:@"%d",count];
    while (![self.countArray containsObject:countStr]) {
        count ++;
        countStr = [NSString stringWithFormat:@"%d",count];
    }
    [self changeDataWithPlayDays:model.value planCount:countStr];

    NSInteger index = [self.countArray indexOfObject:countStr];
    [self.countPickerView setSelectIndex:index];
    [self.countPickerView reloadData];
}
- (void)cheackDayPickerWithModel:(BRResultModel*)model{
    int day = (ceilf)(self.allCount.floatValue/model.value.floatValue);
    NSString *dayStr = [NSString stringWithFormat:@"%d",day];
    while (![self.dayArray containsObject:dayStr]) {
        day ++;
        dayStr = [NSString stringWithFormat:@"%d",day];
    }
    [self changeDataWithPlayDays:dayStr planCount:model.value];
    NSInteger index = [self.dayArray indexOfObject:dayStr];
    [self.dayPickerView setSelectIndex:index];
    [self.dayPickerView reloadData];
}

#pragma mark   日期改变
- (void)changeDataWithPlayDays:(NSString *)day planCount:(NSString *)planCount{
    if (self.dataChangeBlock) {
        self.dataChangeBlock(day,planCount);
    }
}
- (void)updateDayList:(NSMutableArray *)dayList countList:(NSMutableArray *)countList {
    self.dayArray = dayList.mutableCopy;
    self.countArray = countList.mutableCopy;
    [self changeDataWithPlayDays:dayList.firstObject planCount:countList.firstObject];
    
    self.dayPickerView.dataSourceArr = dayList;
    [self.dayPickerView reloadData];
    self.countPickerView.dataSourceArr = countList;
    [self.countPickerView reloadData];
}

- (void)updateDayList:(NSMutableArray *)dayList countList:(NSMutableArray *)countList count:(NSString *)count {
    self.dayArray = dayList.mutableCopy;
    self.countArray = countList.mutableCopy;
    
    self.dayPickerView.dataSourceArr = dayList;
    self.countPickerView.dataSourceArr = countList;
    
    BRResultModel *model = [BRResultModel new];
    model.value = count;
    [self cheackDayPickerWithModel:model];
    
    NSInteger index = [self.countArray indexOfObject:count];
    [self.countPickerView setSelectIndex:index];
    [self.countPickerView reloadData];
        

}
@end
